<!DOCTYPE html><html lang="ja"><head>
<meta charset="utf-8" />
<title>benchmark msgpack vs json</title>
<script src="../msgpack.js"></script>
<script src="uupaa.js"></script>
<script>
var solid10 = {
    "0": "0\u0000\u0000\u0000\u0000",
    "1": {
        "1": false
    },
    "2": null,
    "3": null,
    "4": 0.032400000294840005,
    "5": [
        {
            "5": {
                "5":5
            }
        },
        false,
        false
    ],
    "6": {
        "6": -6
    },
    "7": -0.05670000051597,
    "8": 8,
    "9": 9
};
</script>
<script src="10000.js"></script>
<script src="100000.js"></script>
<script>


uu.ready(function() {
    if (uu.ie678) {
        uu.id("Biggest").disabled = true;
    }
});

var mix = null;

// msgpack.pack + unpack
function test(json) {

    var encoded, decoded;

    function times() {
        var perf = +new Date;
        var perfPoint;
        if (json) {
            if (window.JSON) {
                encoded = JSON.stringify(mix); // NativeJSON

                perfPoint = +new Date;
                uu.log("JSON.stringify = @ms", perfPoint - perf);

                decoded = JSON.parse(encoded);

                uu.log("JSON.parse = @ms", (+new Date) - perfPoint);
            } else {
                encoded = uu.json(mix);
                decoded = uu.json.decode(encoded);
            }
        } else {
            encoded = msgpack.pack(mix);

            perfPoint = +new Date;
            uu.log("msgpack.pack = @ms", perfPoint - perf);

            decoded = msgpack.unpack(encoded);

            uu.log("msgpack.unpack = @ms", (+new Date) - perfPoint);
        }
        return  (+new Date) - perf;
    }

    // try 3 times
//    var rv = [times(), times(), times()];
    var rv = [times()];

//    rv.sort().pop();
//    rv.shift();

    if (json) {
        uu.log("JSON = @ms", rv[0]);
    } else {
        uu.log("msgpack = @ms", rv[0]);
    }

    // verify
    if (1) {
        if (!uu.like(mix, decoded)) {
            uu.log("verify error !");
        }
    }
}

// create random data
function create(size) { // @param Number:
    function addItem() {
        switch ((Math.random() * 12) | 0) {
        case 0: return null; break;
        case 1: return false; break;
        case 2: return true; break;
        case 3: return i.toString(16) + String.fromCharCode(i & 0xffff, i & 0xffff, i & 0xffff, i & 0xffff); break;
        case 4: return i; break;
        case 5: return -i; break;
        case 6: return i / 123.456789; break;
        case 7: return -(i / 123.456789); break;
        case 8: return [addItem(), addItem(), addItem()];
        }
        var r = {};
        r[i] = addItem();
        return r;
    }
    var obj = {}, i = 0, v, type, json;

    for (i = 0; i < size; ++i) {
        obj[i] = addItem();
    }
    if (0) {
        uu.id("json").value = uu.json(obj, true);
    } else {
        uu.id("json").value = "(*snip*)";
    }
    uu.log("data size = " + size);

    mix = obj;
}

function solid(solidData, dataSize) {
    mix = solidData;
    uu.id("json").value = "(*snip*)";
    uu.log("data size = " + dataSize);
}
</script>
</head><body>
<table>
<tr>
    <td>Prepare Data</td>
    <td></td>
    <td>Bench !</td>
</tr>
<tr>
    <td>
        <input type="button" value="Load 100000" onclick="solid(solid100000, 100000)" />
        <input type="button" value="Load 10000" onclick="solid(solid10000, 10000)" />
        <input type="button" value="Load 10" onclick="solid(solid10, 10)" />
        <br />
        <input type="button" id="Biggest" value="Create 100000" onclick="create(100000)" />
        <input type="button" value="Create 10000" onclick="create(10000)" />
        <input type="button" value="Create 10" onclick="create(10)" />
    </td>
    <td>
        -&gt;
    </td>
    <td>
        <input type="button" value="JSON.stringify + JSON.parse" onclick="test(1)" />
        <br />
        <input type="button" value="msgpack.pack + msgpack.unpack" onclick="test(0)" />
    </td>
</tr>
</table>
<textarea id="json" cols="40" rows="2"></textarea>
<input type="button" value="clear log" onclick="uu.log.clear()" />








</body></html>
